int remove_name_from_parent(MINODE* parent, ulong inode, char* name)
{
	int i, j, device = 0, prvRecLen = 0;
	char buf[MAX_BLOCK_SIZE], *pnt;
	EXT2_DISK* diskInfo;
	SUPER super;
	
	if (parent->mounted == true)
		device = parent->mountedDevice->handle;
	else
		device = parent->location.deviceHandle;
		
	if (get_device_info(device, &diskInfo) == -1)
		return -1;
	
	/* Look through direct data blocks */
	for (i = 0; i < NUM_DIRECT_BLOCKS; i++)
	{
		if (parent->inode.i_block[i] != 0)
		{
			if (read_blk(device, parent->inode.i_block[i], diskInfo->blockSize, buf) == -1)
				return -1;
				
			pnt = buf; j = 0;
			while (j < diskInfo->blockSize)
			{
				if (((DIR*)pnt)->inode == inode)
				{
					/* First entry */
					if (j == 0)
						((DIR*)pnt)->inode = 0;
					/* Absort into previous entry */
					else
						((DIR*)(pnt - prvRecLen))->rec_len += ((DIR*)pnt)->rec_len;
					
					if (write_blk(device, parent->inode.i_block[i], diskInfo->blockSize, buf) == -1)
						return -1;					
					return 1;
				}
				prvRecLen = ((DIR*)pnt)->rec_len;
				j += ((DIR*)pnt)->rec_len;
				pnt += ((DIR*)pnt)->rec_len;
			}
		}
	}
}

int do_rmdir(char* pathName)
{
	LOC dirInodeLoc;
	char fullPath[MAX_PATH_LENGTH];
	MINODE* dirInode;
	EXT2_DISK* diskInfo;
	
	if (pathName[0] != '/')
	{
		get_pwd_name(runningProc->cwd, fullPath);
		strcat(fullPath, pathName);
	}
	else
		strcpy(fullPath, pathName);
	
	dirInodeLoc = get_inode_number(fullPath);
	if (dirInodeLoc.inodeNum == 0)
	{
		printf("\"%s\" does not exist\n\n", fullPath);
		return -1;
	}
	
	dirInode = get_inode(dirInodeLoc.deviceHandle, dirInodeLoc.inodeNum);
	if (dirInode == 0)
		return -1;
		
	if (S_ISDIR(dirInode->inode.i_mode))
	{
		if ((runningProc->uid != 0) && (runningProc->uid != dirInode->inode.i_uid))
		{
			printf("Current process does not have access to directory\n\n");
			return -1;
		}
		
		if ((dirInode->busy == true) || (dirInode->mounted == true))
		{
			printf("Cannot rmdir, directory is busy\n\n");
			return -1;
		}
		
		if (dirInode->inode.i_links_count == 2)
		{
			char buf[MAX_BLOCK_SIZE], *pnt = buf;
			
			if (get_device_info(dirInode->location.deviceHandle, &diskInfo) == -1)
				return -1;
			
			if (read_blk(dirInode->location.deviceHandle, dirInode->inode.i_block[0], diskInfo->blockSize, buf) == -1)
				return -1;
				
			pnt += ((DIR*)pnt)->rec_len;
			if (((DIR*)pnt)->rec_len == diskInfo->blockSize - 12)
			{
				int i;
				char baseName[EXT2_MAX_FILE_LENGTH];
				MINODE* parent;
				
				parent = get_inode(dirInode->location.deviceHandle, ((DIR*)pnt)->inode);
				
				base_name(fullPath, baseName);
				
				if (remove_name_from_parent(parent, dirInode->location.inodeNum, baseName) == -1)
					return -1;
					
				for (i = 0; i < NUM_DIRECT_BLOCKS; i++)
				{
					if (dirInode->inode.i_block[i] != 0)
						deallocate_block(dirInode->location.deviceHandle, dirInode->inode.i_block[i]);
				}
				deallocate_inode(dirInode->location.deviceHandle, dirInode->location.inodeNum);
				
				dirInode->refCount = 0;
				
				parent->inode.i_links_count--;
				parent->inode.i_atime = time(0);
				parent->dirty = true;
				
				put_inode(parent);
				return 1;
			}
		}
		
		printf("Cannot rmdir, directory is not empty\n\n");
		return -1;
	}
	
	printf("\"%s\" is not a directory\n\n", pathName);
	return -1;
}
